<?php
	include("pagina_restrita.php"); 
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
	<title> Fagammon - Alterar Ministrator</title>
	<script src="js/SpryValidationTextField.js" type="text/javascript"></script>
	<script src="js/SpryValidationSelect.js" type="text/javascript"></script>
	<link href="js/SpryValidationTextField.css" rel="stylesheet" type="text/css" />
	<link href="js/SpryValidationSelect.css" rel="stylesheet" type="text/css" />
</head>

<body>
	<div id="conteudo">
		
		<!-- chamando o cabeçalho -->
		<?php include ("header.php"); ?>
		
			<div class="quadro">
				<div class="titulo">Alterar Ministrator</div>
				<form id='cadastrar_disciplina' method='post' action='' >
				
				<?php 
					$id =(int)$_GET['id'];
						
					// abre conexão com banco de dados
					require("funcoesBD.php");
					$conn = conectarBancoDados();
					$coordenador = $_SESSION['id_coordenador'];
						
					$result = mysql_query("SELECT id_ministrator, disciplina, nome_professor, sobrenome_professor, ministrator.id_professor, ministrator.id_disciplina FROM ministrator JOIN disciplina ON disciplina.id_disciplina = ministrator.id_disciplina 
										JOIN professor ON professor.id_professor = ministrator.id_professor WHERE id_ministrator = '$id' AND professor.id_coordenador = '$coordenador'
										ORDER BY disciplina ASC");

					$row = mysql_fetch_array($result);
					$qtd_registro = mysql_num_rows($result);
						
					if($qtd_registro > 0) {
				?>
				
					<div class="separador_inpust">
						<label>* Professor:</label>
						<select name="professor" id="select">
				    	<option value="<?php echo $row['id_professor']; ?>"><?php echo $row['nome_professor']." ".$row['sobrenome_professor']; ?></option>
				        <?php					         
					         $sql = "SELECT * FROM professor WHERE id_coordenador = '$coordenador' ORDER BY nome_professor ASC";
					         $qr = mysql_query($sql);
					         
					         while($ln = mysql_fetch_assoc($qr)) {
					            echo '<option value="'.$ln['id_professor'].'">'.$ln['nome_professor'].' '.$ln['sobrenome_professor'].'</option>';
					         }
				      	?>
				  		</select>
					</div> 
					
					<div class="separador_inpust">
						<label>* Disciplina:</label>
						<select name="disciplina" id="select">
				    	<option value="<?php echo $row['id_disciplina']; ?>"><?php echo $row['disciplina']; ?></option>
				        <?php					         
					         $sql = "SELECT id_disciplina, disciplina FROM disciplina WHERE id_coordenador = '$coordenador' ORDER BY disciplina ASC";
					         $qr = mysql_query($sql);
					         
					         while($ln = mysql_fetch_assoc($qr)) {
					            echo '<option value="'.$ln['id_disciplina'].'">'.$ln['disciplina'].'</option>';
					         }
				      	?>
				  		</select>
					</div>

					<div class="separador_inpust">
						<input type="submit" value="Enviar" />
					</div>
					
				</form>
			</div>
			
			<?php 
 
				$disciplina		 = (int)$_POST['disciplina'];
				$professor		 = (int)$_POST['professor'];
				
				if($disciplina && $professor  != "") {

					$sql = ("UPDATE ministrator SET id_disciplina ='$disciplina',id_professor ='$professor' WHERE id_ministrator = '$id'");
					
					if(inserirNoBanco($sql)) {
						header("Location: ministrator.php?status=sucesso");
					} else {
						?> <script>
							alert("Falha ao atualizar os dados.);
						</script> <?php 
					}
					// fechar conexão com o banco de dados
					mysql_close($conn);
				}	
				
				} else {
					echo 'Você não tem permissão para fazer isso!';
				}
			?>
					
			<!-- bibliotecas javaScript para validação -->
			<script type="text/javascript">
			var sprytextfield1 = new Spry.Widget.ValidationTextField("sprytextfield1", "none");
			var spryselect1 = new Spry.Widget.ValidationSelect("spryselect1", {invalidValue:"null"});
			</script>	
				
		<!-- chamando o rodapé -->
		<?php include ("footer.php"); ?>
	</div>
</body>
</html>